﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using JM.JCY.Proxy;
using JM.JCY.BusinessEntity;
using System.Collections;
using Microsoft.SharePoint;

namespace JM.JCY.SharePointComponent
{
    public class CommonFunction
    {
        public static bool GetLawArchivesRole(string _userName,int deptId)
        {
          

            using (UserManagementProxy proxy = new UserManagementProxy())
            {
                bool isDeptMgr = false;
                UserInfoDataSet.T_DeptMgrDataTable dtDeptMgr = proxy.GetUserDeptMgr(_userName);
                foreach (UserInfoDataSet.T_DeptMgrRow dr in dtDeptMgr.Rows)
                {
                    if (dr.DeptId == deptId)
                        isDeptMgr = true;
                }

                if (GetLawArchivesRole2(_userName))
                    isDeptMgr = true;

                return isDeptMgr;
            }
        }
        public static bool GetLawArchivesRole2(string _userName)
        {
            using (UserManagementProxy userProxy = new UserManagementProxy())
            {
                int userId = 0;
                DataSetUserManagement.T_UserInfoDataTable userDT = userProxy.GetUser(0, _userName);
                if (userDT.Rows.Count > 0)
                    userId = userDT[0].ID;

                DepartmentTitleDataSet.T_DeptTitleDataTable dt = userProxy.GetDeptTitleByUserId(userId);
                foreach (DepartmentTitleDataSet.T_DeptTitleRow dr in dt.Rows)
                {
                    if (dr.JobTitle.Contains("专职委员") || dr.JobTitle.Contains("纪检组长"))
                    {
                        return true;
                    }
                }

                return false;

            }
        }
        public static bool IsApplicationAdmin(int appId)
        {
            SPWeb CurrentWeb = SPContext.Current.Web;
            string _userName = CurrentWeb.CurrentUser.LoginName.ToString().Substring(CurrentWeb.CurrentUser.LoginName.LastIndexOf("|") + 1);

            bool isAdmin = false;
             using(ApplicationProxy proxy = new ApplicationProxy())
             {
                ApplicationDataSet.T_ApplicationRoleDataTable dt = proxy.GetApplicationRoleByAppId(appId);

                foreach (ApplicationDataSet.T_ApplicationRoleRow dr in dt.Rows)
                {
                    if (dr.UserName == _userName)
                        isAdmin = true;
                    if (dr.DefaultRole == "Y")
                        isAdmin = true;
                }
            
            }

            return isAdmin;
        }


             
    }
}
